By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 Homemade NAS.md

View raw Download
text/x-script.python • 9.71 kiB
Python script, Unicode text, UTF-8 text executable

title: Cheap, homemade NAS with Raspberry Pi topics: ["hardware", "server", "networking", "raspberry pi", "nas", "gnu/linux"] ---

This is a very simple, cheap and quick way to get networked storage at home. It should not cost more than €120 for all the components (assuming you've got a network you can plug it into). It also offers more flexibility than a commercial NAS, because you can install any software you want on it. And if you already use the Raspberry Pi for something else, you can just add this to it and not worry about an extra device you need power, networking, space and maintenance for.

A Raspberry Pi is already 4-bay since it has 4 USB ports. You can use a hub for more drives, since no HDD will use the full potential of USB 3.0. Just keep in mind you need an adapter for each.

However, for large-scale use, this method should be done with a different type of computer, not a Raspberry Pi.

I assume you know some things about GNU/Linux, SSH, and networking. This guide is not for that; if you don't know these things read some material when you need it.

If you think this is for you, keep reading.

Materials

  • Raspberry Pi, or another small computer that can run GNU/Linux, has a network and USB. I'm using the Pi 4 with 8GB of RAM, but you can use any model.

  • microSD card of at least 16GB (this won't be your primary storage but a boot device)

  • SATA drive(s), or USB drives - SATA drives are cheaper. Format the drive on your own computer; it's easier that way. Choose ext4 as the filesystem.

  • Suitable adapter(s) for the SATA drive(s).

    • For Raspberry Pi 5 you could get a SATA HAT as it has PCIe, which is more efficient.

    • If your other kind of small computer has SATA, you can use that.

    • Otherwise get a USB to SATA adapter, one per drive. For older Raspberry Pis, don't get a SATA HAT, as they use USB as well but cost more for some reason. Please make sure to get one that can take power from a separate source if you're going to use 3.5" drives.

  • A power supply for your computer. For Raspberry Pi, a phone charger of 5V and 3A (marketed as fast charging) is enough.

  • If you're using 3.5" SATA drives, a power supply for the adapter(s) if they don't come with one.

    1. 5" drives need more power than USB can provide.

  • If you want to use wired networking, a cable. If you want to use wireless networking and your computer doesn't have Wi-Fi, some Wi-Fi adapter.

  • If you have an SBC you should have a case, but it's not required. You can also use lego.

  • Some other computer. An Android phone is fine; just install Termux.

My setup

  • Raspberry Pi 4 with 8GB of RAM (€80)

  • No-name 3.5" SATA to USB adapter with power supply (€18)

  • One Toshiba P300 4TB 3.5" SATA drive (€90). No RAID, but I copy important files to my own PC.

  • Raspberry Pi 15W power adapter (€9)

  • A 1.5m Cat6 cable (€3)

  • SanDisk Extreme 128GB microSD card (€15)

  • Official case (€6). Don't judge me, it's just a piece of plastic, and it's cheap. Don't forget Raspberry Pi is not Apple.

Total: €221. Now, this is not the cheapest setup. I use that Pi for other things as well. You can certainly have a cheaper setup:

Low-cost setup

  • Raspberry Pi 4 with 1GB of RAM (€40). I'd insist on getting a big Pi, as the Zero doesn't have wired networking, only one USB port, and too little RAM.

  • Cheaper no-name 3.5" SATA to USB adapter with power supply (€10)

  • One Toshiba P300 1TB 3.5" SATA drive (€50)

  • Phone charger (€6, if not already owned)

  • No cable, use Wi-Fi (I assume you have Wi-Fi)

  • SanDisk Ultra 32GB microSD card (€7)

  • No case

Total: €113, with drive included!

What about a "real" NAS?

Cheapest NAS I could find is €180! It is 1-bay and has 1GB of RAM. It doesn't do anything else, it doesn't use standard protocols, it runs a proprietary OS, it's bulkier and much more expensive. (The brand is Synology.)

No, it doesn't come with drives. Using the same drive as above, it would reach €230.

Well, it's plug-and-play but that makes it much more restricted. Also, it does have a warranty and professional support. However, it's not the kind of NAS a business would use either.

Setup (skip if you already have the computer set up)

  1. Flash your microSD card. Raspberry Pi recommends their own (free) software, Choose an OS without a GUI: Raspberry Pi OS Lite, Ubuntu enable SSH and preset the Wi-Fi settings if you're going

  2. Put the card in the computer, plug in the network and start it.

  3. Find out the IP address of the computer. You can use your router's web interface, or a network Now is a good time to set up a static IP address for the computer, forward its ports dynamic DNS if you want to access it from the internet. YDNS is a imple and free service for that. Their official client is also free software.

  4. Connect to your server via SSH. On GNU/Linux or Macintosh you can use the preinstalled ssh On Windows, you can use PuTTY.

  5. Set a root password with sudo passwd. Then su.

  6. Update the system with apt update && apt -y upgrade.

Set up the drives

You must edit the /etc/fstab to automatically mount the drives on boot. If you know how to do this, you can do it yourself. For the rest of the tutorial the mount point will be /storage.

  1. Turn off the computer and plug in the drive(s).

  2. Turn on the computer.

  3. lsblk to find the drive(s). They should be /dev/sd followed by a letter. For now we'll

  4. Find the drive's UUID with blkid /dev/sdX1.

  5. Add the drive to the /etc/fstab as such:

    =00000000-0000-0000-0000-000000000000 /storage ext4 defaults 0 2

Replace the UUID with the one you found and, if you want, the mount point (/storage in this case) with another one, and the filesystem to match the one you formatted the drive with. Change the last column to 0 to disable fsck for that drive.

  1. mkdir /storage to create the mount point.

  2. Reboot.

Give each user their private directory

We'll mirror the home layout. However, we won't move the existing home directories, because then users can't use the SD card, and you may have some other apps to run on the server that you want to be separate from the mass storage.

Write a systemd service to create the directories on boot. Give it a name inside /etc/systemd/system/, like /etc/systemd/system/setup_storage_dirs.service

[Unit]
Description=Update private user storage spaces

[Service]
Type=oneshot
ExecStart=/usr/bin/python3 /usr/local/bin/setup_storage_dirs.py

[Install]
WantedBy=multi-user.target

And the script /usr/local/bin/setup_storage_dirs.py:

#!/bin/python3

import os
import subprocess
from pathlib import Path

homes = Path("/home").glob("*")
storage_path = Path("/storage")   # change this if you changed the mount point

for home in homes:
   if home.is_dir():
       user = home.name
       storage = storage_path/user
       bound = home/"Storage"    # change this if you'd like a different name
       if not storage.exists():
           os.makedirs(storage)
           os.chown(storage, home.stat().st_uid, home.stat().st_gid)
           os.chmod(storage, 0o700)
       if not bound.is_dir():
           os.makedirs(bound)
           os.chown(bound, home.stat().st_uid, home.stat().st_gid)
           os.chmod(bound, 0o700)
       
       # Using bind mounts; some file managers don't like symlinks
       subprocess.run(["mount", "--bind", str(storage), str(bound)])

Reboot. Now each user has a private directory on your drive at ~/Storage. You can add more users; each will get their own directory only they can access.

Using the server

This guide does not cover setting up sharing protocols. However, we will use SFTP (FTP over SSH) because it's plug-and-play, the speed difference is negligible, it integrates with system users, it's native (works in the system file manager, no web needed) and, last but not least, it's encrypted.

Since you set up the OS to use SSH, you can use SFTP. All file managers can connect to SFTP servers, except for Windows Explorer, in which case you can use WinSCP. This is not my problem, it's Microsoft's problem. Even the preinstalled file manager on Samsung phones can use an SFTP add-on. Not like Next"cloud"¹ is more integrated.

If you want to mount it there is the sshfs program as well. This is useful if you don't like GUI file managers or want easier access to the files.

SFTP in the terminal

sftp -oPort=22 user@server

If you use port 22, you can omit the oPort option. The commands are identical to the ones in the classic ftp client.

SSHFS

sshfs user@server:/storage/user ~/server

Second argument is the "mount point". If you want to unmount it:

fusermount -u ~/server

SFTP in the file manager

Graphically, you can access SFTP by typing sftp://user@server in the address bar (works in most cases) or by finding an option to connect to a server.

Nemo (GNU/Linux)

File > Connect to server > select type SSH

Material Files (Android)

Menu > Add storage > SFTP server

Amaze (Android)

Plus button > Cloud connection > SCP/SFTP Connection

Windows

Windows Explorer doesn't natively support SFTP. WinSCP is a good client for it, and it's free software.

Samsung phone file manager (Android)

Storage space section > Network storage space > Update the app when prompted > Plus > SFTP server

                
                    
1
---
2
title: Cheap, homemade NAS with Raspberry Pi
3
topics: ["hardware", "server", "networking", "raspberry pi", "nas", "gnu/linux"]
4
---
5
6
This is a very simple, cheap and quick way to get networked storage at home. It should not cost
7
more than €120 for all the components (assuming you've got a network you can plug it into). It also
8
offers more flexibility than a commercial NAS, because you can install any software you want on it.
9
And if you already use the Raspberry Pi for something else, you can just add this to it and not
10
worry about an extra device you need power, networking, space and maintenance for.
11
12
A Raspberry Pi is already 4-bay since it has 4 USB ports. You can use a hub for more drives,
13
since no HDD will use the full potential of USB 3.0. Just keep in mind you need an adapter for each.
14
15
However, for large-scale use, this method should be done with a different type of computer, not
16
a Raspberry Pi.
17
18
I assume you know some things about GNU/Linux, SSH, and networking. This guide is not for that;
19
if you don't know these things read some material when you need it.
20
21
If you think this is for you, keep reading.
22
23
Materials
24
---------
25
26
* Raspberry Pi, or another small computer that can run GNU/Linux, has a network and USB. I'm using
27
the Pi 4 with 8GB of RAM, but you can use any model.
28
* microSD card of at least 16GB (this won't be your primary storage but a boot device)
29
* SATA drive(s), or USB drives - SATA drives are cheaper. Format the drive on your own computer;
30
it's easier that way. Choose ext4 as the filesystem.
31
* Suitable adapter(s) for the SATA drive(s).
32
* For Raspberry Pi 5 you could get a SATA HAT as it has PCIe, which is more efficient.
33
* If your other kind of small computer has SATA, you can use that.
34
* Otherwise get a USB to SATA adapter, one per drive. For older Raspberry Pis, don't get a
35
SATA HAT, as they use USB as well but cost more for some reason. Please make sure to get one
36
that can take power from a separate source if you're going to use 3.5" drives.
37
* A power supply for your computer. For Raspberry Pi, a phone charger of 5V and 3A (marketed
38
as fast charging) is enough.
39
* If you're using 3.5" SATA drives, a power supply for the adapter(s) if they don't come with one.
40
3.5" drives need more power than USB can provide.
41
* If you want to use wired networking, a cable. If you want to use wireless networking and your
42
computer doesn't have Wi-Fi, some Wi-Fi adapter.
43
* If you have an SBC you should have a case, but it's not required. You can also use lego.
44
* Some other computer. An Android phone is fine; just install [Termux](https://termux.com/).
45
46
### My setup
47
48
* Raspberry Pi 4 with 8GB of RAM (€80)
49
* No-name 3.5" SATA to USB adapter with power supply (€18)
50
* One Toshiba P300 4TB 3.5" SATA drive (€90). No RAID, but I copy important files to my own PC.
51
* Raspberry Pi 15W power adapter (€9)
52
* A 1.5m Cat6 cable (€3)
53
* SanDisk Extreme 128GB microSD card (€15)
54
* Official case (€6). Don't judge me, it's just a piece of plastic, and it's cheap. Don't forget
55
Raspberry Pi is not Apple.
56
57
Total: €221. Now, this is not the cheapest setup. I use that Pi for other things as well. You can
58
certainly have a cheaper setup:
59
60
### Low-cost setup
61
62
* Raspberry Pi 4 with 1GB of RAM (€40). I'd insist on getting a big Pi, as the Zero doesn't
63
have wired networking, only one USB port, and too little RAM.
64
* Cheaper no-name 3.5" SATA to USB adapter with power supply (€10)
65
* One Toshiba P300 1TB 3.5" SATA drive (€50)
66
* Phone charger (€6, if not already owned)
67
* No cable, use Wi-Fi (I assume you have Wi-Fi)
68
* SanDisk Ultra 32GB microSD card (€7)
69
* No case
70
71
Total: €113, with drive included!
72
73
What about a "real" NAS?
74
75
Cheapest NAS I could find is €180! It is 1-bay and has 1GB of RAM. It doesn't do anything else,
76
it doesn't use standard protocols, it runs a proprietary OS, it's bulkier and much more expensive.
77
(The brand is Synology.)
78
79
No, it doesn't come with drives. Using the same drive as above, it would reach €230.
80
81
Well, it's plug-and-play but that makes it much more restricted. Also, it does have a warranty
82
and professional support. However, it's not the kind of NAS a business would use either.
83
84
Setup (skip if you already have the computer set up)
85
----------------------------------------------------
86
87
1. Flash your microSD card. Raspberry Pi recommends [their own (free) software](https://www.raspberrypi.org/software/),
88
but there are other methods as well. Choose an OS without a GUI: Raspberry Pi OS Lite, Ubuntu
89
Server or plain Debian. Make sure to enable SSH and preset the Wi-Fi settings if you're going
90
to use Wi-Fi.
91
2. Put the card in the computer, plug in the network and start it.
92
3. Find out the IP address of the computer. You can use your router's web interface, or a network
93
scanner. Now is a good time to set up a static IP address for the computer, forward its ports
94
and get dynamic DNS if you want to access it from the internet. [YDNS](https://ydns.io/) is a
95
really simple and free service for that. Their official client is also free software.
96
4. Connect to your server via SSH. On GNU/Linux or Macintosh you can use the preinstalled `ssh`
97
client. On Windows, you can use [PuTTY](https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html).
98
5. Set a root password with `sudo passwd`. Then `su`.
99
6. Update the system with `apt update && apt -y upgrade`.
100
101
Set up the drives
102
-----------------
103
104
You must edit the `/etc/fstab` to automatically mount the drives on boot. If you know how to do
105
this, you can do it yourself. For the rest of the tutorial the mount point will be `/storage`.
106
107
1. Turn off the computer and plug in the drive(s).
108
2. Turn on the computer.
109
3. `lsblk` to find the drive(s). They should be `/dev/sd` followed by a letter. For now we'll
110
only take car of one drive, but you can do the same for multiple drives.
111
4. Find the drive's UUID with `blkid /dev/sdX1`.
112
5. Add the drive to the `/etc/fstab` as such:
113
```
114
UUID=00000000-0000-0000-0000-000000000000 /storage ext4 defaults 0 2
115
```
116
117
Replace the UUID with the one you found and, if you want, the mount point (`/storage` in this
118
case) with another one, and the filesystem to match the one you formatted the drive with. Change
119
the last column to `0` to disable fsck for that drive.
120
6. `mkdir /storage` to create the mount point.
121
7. Reboot.
122
123
Give each user their private directory
124
--------------------------------------
125
126
We'll mirror the home layout. However, we won't move the existing home directories, because then
127
users can't use the SD card, and you may have some other apps to run on the server that you want
128
to be separate from the mass storage.
129
130
Write a systemd service to create the directories on boot. Give it a name inside `/etc/systemd/system/`,
131
like `/etc/systemd/system/setup_storage_dirs.service`
132
133
```ini
134
[Unit]
135
Description=Update private user storage spaces
136
137
[Service]
138
Type=oneshot
139
ExecStart=/usr/bin/python3 /usr/local/bin/setup_storage_dirs.py
140
141
[Install]
142
WantedBy=multi-user.target
143
```
144
145
And the script `/usr/local/bin/setup_storage_dirs.py`:
146
147
```python
148
#!/bin/python3
149
150
import os
151
import subprocess
152
from pathlib import Path
153
154
homes = Path("/home").glob("*")
155
storage_path = Path("/storage") # change this if you changed the mount point
156
157
for home in homes:
158
if home.is_dir():
159
user = home.name
160
storage = storage_path/user
161
bound = home/"Storage" # change this if you'd like a different name
162
if not storage.exists():
163
os.makedirs(storage)
164
os.chown(storage, home.stat().st_uid, home.stat().st_gid)
165
os.chmod(storage, 0o700)
166
if not bound.is_dir():
167
os.makedirs(bound)
168
os.chown(bound, home.stat().st_uid, home.stat().st_gid)
169
os.chmod(bound, 0o700)
170
171
# Using bind mounts; some file managers don't like symlinks
172
subprocess.run(["mount", "--bind", str(storage), str(bound)])
173
```
174
175
Reboot. Now each user has a private directory on your drive at `~/Storage`. You can add more
176
users; each will get their own directory only they can access.
177
178
Using the server
179
----------------
180
181
This guide does not cover setting up sharing protocols. However, we will use SFTP (FTP over SSH)
182
because it's plug-and-play, the speed difference is negligible, it integrates with system users,
183
it's native (works in the system file manager, no web needed) and, last but not least, it's encrypted.
184
185
Since you set up the OS to use SSH, you can use SFTP. All file managers can connect to SFTP
186
servers, except for Windows Explorer, in which case you can use WinSCP. This is not my problem,
187
it's Microsoft's problem. Even the preinstalled file manager on Samsung phones can use an SFTP
188
add-on. Not like Next"cloud"¹ is more integrated.
189
190
If you want to mount it there is the `sshfs` program as well. This is useful if you don't like
191
GUI file managers or want easier access to the files.
192
193
### SFTP in the terminal
194
195
```sh
196
sftp -oPort=22 user@server
197
```
198
199
If you use port 22, you can omit the oPort option. The commands are identical to the ones in the
200
classic `ftp` client.
201
202
### SSHFS
203
204
```sh
205
sshfs user@server:/storage/user ~/server
206
```
207
208
Second argument is the "mount point". If you want to unmount it:
209
210
```sh
211
fusermount -u ~/server
212
```
213
214
### SFTP in the file manager
215
216
Graphically, you can access SFTP by typing `sftp://user@server` in the address bar (works in most
217
cases) or by finding an option to connect to a server.
218
219
#### Nemo (GNU/Linux)
220
File > Connect to server > select type `SSH`
221
222
#### Material Files (Android)
223
Menu > Add storage > SFTP server
224
225
#### Amaze (Android)
226
Plus button > Cloud connection > SCP/SFTP Connection
227
228
#### Windows
229
Windows Explorer doesn't natively support SFTP. WinSCP is a good client for it, and it's free
230
software.
231
232
#### Samsung phone file manager (Android)
233
Storage space section > Network storage space > Update the app when prompted > Plus > SFTP server
234